//
//  FCRelationTyleSheetViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/18.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCRelationTyleSheetViewController.h"
#import "TopNoticeLabel.h"
#import "CustomerRelationHeader.h"

@interface FCRelationTyleSheetViewController ()
@property (weak, nonatomic) IBOutlet UIView *topNoticeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNoticeHeagihConstraint;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightConstraint;

@end

@implementation FCRelationTyleSheetViewController
    
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
    
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
        
    return UIStatusBarStyleLightContent;
}

- (void)setupUI{
    self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    TopNoticeLabel *view = [[TopNoticeLabel alloc] init];
    view.noticeTxt = customerRelationLocaChanged(@"inventory_product_choose_more", @"可多选");
    // 计算高度
    CGSize maximumLabelSize = CGSizeMake(WJCScreenW - (16 * 2 + 20 + 10), CGFLOAT_MAX);// 16:内边距;20:关闭按钮宽度;10:内边距
    CGSize expectSize = [view.noticeLabel sizeThatFits:maximumLabelSize];
    view.frame = CGRectMake(0, 0, WJCScreenW, expectSize.height + 20);
    [self.topNoticeLabel addSubview:view];
    view.closeBlock = ^{
        [UIView animateWithDuration:0.25 animations:^{
            _topNoticeHeagihConstraint.constant = 0;
        } completion:nil];
    };
    
    CGFloat W = (WJCScreenW - 15 - 16 * 3) / 3;
    CGFloat H = IphonePlus ? 48 : 32;
    NSInteger rank = 3;
    CGFloat rankMargin = 15;
    CGFloat rowMargin = 16;
    
    // 重现排序，优先排列需要的类型
    if (self.priorityRelationTypeArr.count > 0) {
        self.priorityRelationTypeArr = (NSMutableArray *)[[self.priorityRelationTypeArr reverseObjectEnumerator] allObjects];
        for (FCRelationTypeModel *priorityType in self.priorityRelationTypeArr) {
            for (int i = 0; i < self.relationTypeArr.count; i++) {
                FCRelationTypeModel *relationType = self.relationTypeArr[i];
                
                if ([priorityType.Id isEqual:relationType.Id]) {
                    [self.relationTypeArr removeObject:relationType];
                    [self.relationTypeArr insertObject:relationType atIndex:0];
                    break;
                }
            }
        }
    }
    
    for (int i = 0; i < self.relationTypeArr.count; i++) {
        FCRelationTypeModel *relationTypeModel = self.relationTypeArr[i];
        
        UIButton *relationTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:relationTypeBtn];
        relationTypeBtn.tag = 100 + i;
        [relationTypeBtn setTitle:relationTypeModel.name forState:UIControlStateNormal];
        [relationTypeBtn setTitleColor:FCTxtColor forState:UIControlStateNormal];
        [relationTypeBtn setTitleColor:FCBlueTxtColor forState:UIControlStateSelected];
        relationTypeBtn.titleLabel.font = font14;
        BA_ViewBorderRadius(relationTypeBtn, 2, 0, FCLineColor);
        [relationTypeBtn setBackgroundImage:customerRelationImageName(@"ic_selected_many") forState:UIControlStateSelected];
        relationTypeBtn.backgroundColor = FCBackColor;
        CGFloat X = (i % rank) * (W + rankMargin);
        NSUInteger Y = (i / rank) * (H +rowMargin);
        CGFloat top = 15;
        CGFloat left = 16;
        relationTypeBtn.frame = CGRectMake(X+left, Y+top, W, H);
        relationTypeBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
        [relationTypeBtn addTarget:self action:@selector(selRelationType:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (self.relationTypeArr.count % rank > 0) {
        self.contentViewHeightConstraint.constant = (self.relationTypeArr.count / rank + 1) * (H + rowMargin) + 15;
    } else {
        self.contentViewHeightConstraint.constant = self.relationTypeArr.count / rank * (H + rowMargin) + 15;
    }
    
    if (self.selectedRelationTypeArr.count > 0) {
        [self defaultSelectedRelationType:self.selectedRelationTypeArr];
    }
    if (self.managerRelationTypeArr.count > 0) {
        [self defaultSelectedRelationType:self.managerRelationTypeArr];
    }
}

- (void)selRelationType:(UIButton *)sender{
    FCRelationTypeModel *relationType = self.relationTypeArr[sender.tag - 100];
    for (FCRelationTypeModel *newRelationType in self.managerRelationTypeArr) {
        if ([relationType.Id isEqualToString:newRelationType.Id]) {
            [SVProgressHUD showInfoWithStatus:customerRelationLocaChanged(@"不能删除已添加分组", @"不能删除已添加分组")];
            return;
        }
    }
    
    
    
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        [self.selectedRelationTypeArr addObject:relationType];
        sender.backgroundColor = FCWhiteColor;
        BA_ViewBorderRadius(sender, 2, 0, FCLineColor);
    } else {
        NSArray *arr = [self.selectedRelationTypeArr mutableCopy];
        for (FCRelationTypeModel *editRelationType in arr) {
            if ([relationType.Id isEqualToString:editRelationType.Id]) {
                [self.selectedRelationTypeArr removeObject:editRelationType];
            }
        }
        
        sender.backgroundColor = FCBackColor;
        BA_ViewBorderRadius(sender, 2, 0, FCLineColor);
    }
}

- (void)defaultSelectedRelationType:(NSArray *)relationTypeArr{
    for (UIView *subView in self.contentView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)subView;
            FCRelationTypeModel *relationType = self.relationTypeArr[subView.tag - 100];
            for (FCRelationTypeModel *newRelationType in relationTypeArr) {
                if ([relationType.Id isEqualToString:newRelationType.Id]) {
                    btn.selected = YES;
                    btn.backgroundColor = FCWhiteColor;
                    BA_ViewBorderRadius(btn, 2, 0, FCLineColor);
                    if (self.selectedRelationTypeArr != relationTypeArr) {
                        NSArray *arr = [self.selectedRelationTypeArr mutableCopy];
                        BOOL isAdd = YES;
                        for (FCRelationTypeModel *selectedRelationType in arr) {
                            if ([relationType.Id isEqualToString:selectedRelationType.Id]) {
                                isAdd = NO;
                            }
                        }
                        if (isAdd) {
                            [self.selectedRelationTypeArr addObject:relationType];
                        }
                        
                    }
                }
            }
        }
    }
}

- (IBAction)completeRelatonTypeSelect:(UIButton *)sender {
    if (self.completeBlock) {
        self.completeBlock(self.selectedRelationTypeArr);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
